package com.jxl.controller;


import cn.hutool.core.bean.BeanUtil;
import com.jxl.constant.JwtClaimsConstant;
import com.jxl.entity.dto.UpdatePasswordDTO;
import com.jxl.entity.dto.UserLoginDTO;
import com.jxl.entity.po.User;
import com.jxl.entity.query.UserPageQuery;
import com.jxl.entity.vo.*;
import com.jxl.properties.JwtProperties;
import com.jxl.service.IUserService;
import com.jxl.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 金小梁
 * @since 2025-06-08
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private IUserService userService;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value="员工登录")
    public ResponseResult<UserLoginVo> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("员工登录：{}", userLoginDTO);

        User user=userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        claims.put(JwtClaimsConstant.ROLE,  user.getRole());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        //封装用户返回信息
        UserLoginVo userLoginVo = UserLoginVo.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .realName(user.getRealName())
                .role(user.getRole())
                .token(token)
                .build();

        return ResponseResult.success(userLoginVo);
    }

    @ApiOperation("分页查询用户")
    @PostMapping("/queryUsers")
    public ResponseResult<PageResponse<UserVO>> queryUsers(@RequestBody UserPageQuery userPageQuery) {
        PageResponse<UserVO> pageResponse = userService.queryUsers(userPageQuery);
        return ResponseResult.success(pageResponse);
    }


    /**
     * 新增用户
     */
    @ApiOperation("新增用户")
    @PostMapping("/add")
    public ResponseResult add(@RequestBody User user) {
        userService.saveUser(user);
        return ResponseResult.success();
    }


    /**
     * 删除用户
     */
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        userService.deleteByIds(ids);
        return ResponseResult.success();
    }

    /**
     * 编辑用户
     */
    @PutMapping("/edit")
    public ResponseResult editUser(@RequestBody User user) {
        userService.updateUser(user);
        return ResponseResult.success();
    }

    /**
     * 查询所有的用户
     */
    @GetMapping("/listAll")
    public ResponseResult<List<UserVO>> listAll() {
        List<User> list = userService.query().eq("role", 3)
                .eq("status", 1)
                .list();
        // 将list集合转换为userVO集合
        List<UserVO> userVOS = BeanUtil.copyToList(list, UserVO.class);
        return ResponseResult.success(userVOS);
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public ResponseResult updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO) {
        userService.updatePassword(updatePasswordDTO);
        return ResponseResult.success();
    }

}
